package br.sales.controller;

import java.util.List;

import br.sales.entity.Configuration;
import br.sales.entity.exception.EntityExistsException;
import br.sales.persistence.IPersistence;
import br.sales.persistence.PersistenceFactory;
import br.sales.persistence.exception.DBContainerNotInitializedException;


public class ConfigurationControllerImpl implements IConfigurationController {

	private static IConfigurationController configurationController;
	
	
	public ConfigurationControllerImpl() {
	}
	
	public static IConfigurationController getInstance(){
		if (configurationController == null){
			synchronized (IConfigurationController.class) {
				if (configurationController == null){
					configurationController = new ConfigurationControllerImpl();
				}
			}
		}
		return configurationController;
	}
	
	@Override
	public Configuration save(Configuration configuration) throws DBContainerNotInitializedException, EntityExistsException{
		IPersistence persistence = PersistenceFactory.createDB4ODAO();
		try{
			Configuration configurationFromDB = retrieveByName(configuration.getName());
			if (configurationFromDB == null){
				configurationFromDB = save(configuration,persistence);
				persistence.commit();
				return configurationFromDB;
			}
			throw new EntityExistsException();
		}catch (DBContainerNotInitializedException e) {
			throw new DBContainerNotInitializedException();
		}
	}

	@Override
	public Configuration save(Configuration configuration,
			IPersistence persistence) throws DBContainerNotInitializedException {
		return (Configuration) persistence.save(configuration);
		
	}

	@Override
	public Configuration update(Configuration configuration) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Configuration update(Configuration configuration,
			IPersistence persistence) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(Configuration configuration) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(Configuration configuration, IPersistence persistence) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Configuration retrieveByName(String name) {
		IPersistence persistence  = PersistenceFactory.createDB4ODAO();
		try{
			Configuration configuration = new Configuration();
			configuration.setName(name);
			return (Configuration) persistence.retrieveByExample(configuration);
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}

	@Override
	public Configuration retrieveByValue(String value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Configuration retrieveByName(String name, IPersistence persistence) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Configuration retrieveByValue(String value, IPersistence persistence) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Configuration> retrieveAll() {
		// TODO Auto-generated method stub
		return null;
	}

}
